import torch
from diffusers import I2VGenXLPipeline
from diffusers.utils import load_image, export_to_gif
import moviepy.editor as mp
from datetime import datetime


repo_id = "ali-vilab/i2vgen-xl" 
pipeline = I2VGenXLPipeline.from_pretrained(repo_id, local_files_only=True, torch_dtype=torch.float16, variant="fp16").to("cuda")

image_url = "20240312025555.jpg"
from PIL import Image
image = Image.open(image_url).convert("RGB")
# prompt = "Man take out pistol"
# prompt = "alien opens its mouth and uses fangs"
prompt = "the rabbit took out the laser he carried on his back and aimed it at an alien "
generator = torch.manual_seed(8888) #8888
frames = pipeline(
    prompt=prompt,
    image=image,
    generator=generator
).frames[0]

ts = datetime.now().strftime("%Y%m%d%H%M%S")
filename = f"{ts}.gif"

gif_file = export_to_gif(frames, filename)
print(gif_file)

clip = mp.VideoFileClip(gif_file)
videofilename = f"{ts}.mp4"
clip.write_videofile(videofilename)